package com.mes.basicdata.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;

import com.bstek.bdf2.core.orm.ParseResult;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.mes.basicdata.domain.Part;

public class TestCode {

	public static void main(String[] args) {
		Criteria criteria = new Criteria();
		
		Map<String, Object> map = new HashMap<>();
		map.put("deptUid", "d001");
		map.put("userName", "zongbu");
		map.put("userType", "100");
		
		Page<Part> page = new Page<>(10, 1);
		
		try {
			new TestCode().queryTeacher(page, map, criteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void queryTeacher(Page<Part> page, Map<String, Object> parameter,
			Criteria criteria) throws Exception {
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from "
				+ Part.class.getName() + " a where 1=1 ");

		if (null != parameter && !parameter.isEmpty()) {
			String deptUid = (String) parameter.get("deptUid");
			if (StringUtils.isNotEmpty(deptUid)) {
				coreHql.append(" and a.dept.id like :uniqueId ");
				args.put("uniqueId", "%" + deptUid + "%");
			}
			
			String userName = (String) parameter.get("userName");
			if (StringUtils.isNotEmpty(userName)) {
				coreHql.append(" and a.user.username like :username ");
				args.put("username", "%" + userName + "%");
			}
			
			
			String userType = (String) parameter.get("userType");
			if (StringUtils.isNotEmpty(userType)) {
				coreHql.append(" and a.type = :userType ");
				args.put("userType", Integer.parseInt(userType));
			}
			String userStatus = (String) parameter.get("userStatus");
			if (StringUtils.isNotEmpty(userStatus)) {
				coreHql.append(" and a.status = :userStatus ");
				args.put("userStatus", Integer.parseInt(userStatus));
			}
			String deptUid2 = (String) parameter.get("deptUid2");
			if (StringUtils.isNotEmpty(deptUid2)) {
				coreHql.append(" and a.uniqueId in (select b.username from UserDept b where b.deptId like :deptId2) ");
				args.put("deptId2", "%" + deptUid2 + "%");
			}
			String positionId = (String) parameter.get("positionId");
			if (StringUtils.isNotEmpty(positionId)) {
				coreHql.append(" and a.uniqueId in (select c.username from UserPosition c where c.positionId like :positionId) ");
				args.put("positionId", "%" + positionId + "%");
			}
		}

//		if (null != criteria) {
//			ParseResult result = this.parseCriteria(criteria, true, "a");
//			if (null != result) {
//				coreHql.append(" and " + result.getAssemblySql());
//				args.putAll(result.getValueMap());
//			}
//		}

		String countHql = "select count(*) " + coreHql.toString();
		String hql = coreHql.toString() + "order by a.dept.id,a.id";
		
		
		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while(iterator.hasNext()){
			key = (String)iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":"+key, ":"+newKey);
			countHql = countHql.replace(":"+key, ":"+newKey);
		}
		
	System.out.println("hql: " + hql);
	System.out.println("countHql: " + countHql);
	System.out.println("newArgs: " + newArgs);
		
//		this.pagingQuery(page, hql, countHql, newArgs);
	}
	

}
